package com.demo.network.bio;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerNormal {

    private static int DEFAULT_PORT = 9092;
    private static ServerSocket server;

    public static void start() throws IOException {
        start(DEFAULT_PORT);
    }

    public synchronized static void start(int port) throws IOException {
        if (server != null) return;
        try {
            server = new ServerSocket(port);
            System.out.println("服务端口已启动，端口号：" + port);
            Socket socket;
            while (true) {
                socket = server.accept();
                new Thread(new ServerHandler(socket)).start();
            }
        } finally {
            if (server != null) {
                System.out.println("服务器已关闭");
                server.close();
                server = null;
            }
        }
    }

}
